<template>
	<view class="u-padding-left-32 u-padding-right-32" :class="currentTheme">
		<a-nabbar :name="`${storeName}设置`" />
		<view class="u-padding-20 u-text-center text-order-time u-font-26">{{authObj.tips}}</view>
		<itemBox paddinglr="40">
			<label-box :label="`${storeName}名`" labelWidth="130" paddingTb="18">
				<u-input v-model="form.nickname" :disabled="disabled" :placeholder="`请输入${storeName}名`" border :clearable="false" 
				:borderColor="currentThemeObj.inputBorderColor" :placeholder-style="`color:${currentThemeObj.placeholderColor}`" maxlength="20"/>
			</label-box>
			<label-box label="所在地区" labelWidth="130" paddingTb="18">
				<select-item v-model="form.address" :disabled="disabled" placeholder="省市区县、乡镇等"  mode="popSelectCity"  @getSelect="getSelectCity"></select-item>
			</label-box>
			<label-box label="详细地址" labelWidth="130" paddingTb="18">
				<u-input :disabled="disabled" v-model="form.detailAddress"  placeholder="街道、楼牌号" border :clearable="false" 
				:borderColor="currentThemeObj.inputBorderColor" :placeholder-style="`color:${currentThemeObj.placeholderColor}`" maxlength="30"/>
			</label-box>
			<label-box label="微信号" labelWidth="130" paddingTb="18">
				<u-input :disabled="disabled" v-model="form.wxId"  placeholder="请输入微信号" border :clearable="false" 
				:borderColor="currentThemeObj.inputBorderColor" :placeholder-style="`color:${currentThemeObj.placeholderColor}`" maxlength="20"/>
			</label-box>
			<template v-if="pageUserType=='store'">
				<label-box label="联系人" labelWidth="130" paddingTb="18">
					<u-input :disabled="disabled" v-model="form.contactMan"  placeholder="请输入联系人" border :clearable="false" 
					:borderColor="currentThemeObj.inputBorderColor" :placeholder-style="`color:${currentThemeObj.placeholderColor}`" maxlength="20"/>
				</label-box>
				<label-box label="联系电话" labelWidth="130" paddingTb="18">
					<u-input :disabled="disabled" v-model="form.contactNumber" type="number" placeholder="请输入联系电话" border :clearable="false" 	maxlength="11"
					:borderColor="currentThemeObj.inputBorderColor" :placeholder-style="`color:${currentThemeObj.placeholderColor}`"/>
				</label-box>
			</template>
			<label-box label="经营年数" labelWidth="130" paddingTb="18">
				<select-item :disabled="disabled" :list="yearList" v-model="form.businessYear" @getIds="getYearIds"></select-item>
			</label-box>
		</itemBox>
		<fixed-team btnText="保存设置" v-if="!disabled" @btnClick="setInfo" :loading="loading"></fixed-team>
	</view>
</template>

<script>
	import {mapGetters} from 'vuex'
	import {userEnterInfoSetting,updateUserType} from '@/api/user.js'
	import selectCityMixins from '@/mixins/selectMixins.js'
	import uploader from '@/components/upload/upload.vue'
	export default {
		mixins:[selectCityMixins],
		data() {
			return {
				form:{
					hotelAddress:'',
					hotelRoomNo:''
				},
				show:false,
				option:{},
				yearList:[],
				loading:false,
				uploadFlag:true,
				pageUserType:'store'
			};
		},
		components: {
			uploader
		},
		computed: { 
			...mapGetters(['loginForm','token']),
			authObj() {
				if(this.pageUserType){
					return {
						issue:{
							tips:'设置发行信息，获得更多曝光'
						},
						store:{
							tips:'设置门店信息，轻松提交意见'
						}
					}[this.pageUserType]
				}
				return {}
			},
			storeName(){
				return {'issue':'发行','store':'门店'}[this.pageUserType]
			},
			disabled(){ //已经认证
				return this.loginForm.userAuthenticate==2
			}
		},
		onShow() {
			if(this.token) this.$store.dispatch("getInfo"); //更新用户信息
		},
		onLoad(option) {
			this.form = {...this.form,...this.$store.getters.loginForm};
			const cityPath = this.form.cityPath
			if(cityPath) this.$set(this.form,'address',this.cityPathFormat(cityPath))
			this.option = option;
			this.$store.dispatch('getClassify').then(res=>{
				const {children} = res.find(val=>val.label=='经营年数') || {}
				this.yearList = children.map(val=>{
					val.value = val.id;
					return val;
				}) || [];
				const year = this.yearList.find(val=>val.id===this.form.businessYear);
				if(year){
					this.$set(this.form,'businessYear',year.label)
				}
			})
			this.pageUserType = option.userType || this.loginForm.userType ||'store'
		},
		methods:{
			async setInfo(){
				if(!this.uploadFlag) {
					this.$u.toast('门店图片上传中')
					return false;
				}
				const {wxId,nickname,contactNumber,address} = this.form;
				//const wxreg = /^[(a-zA-Z)|_]{1}[-_a-zA-Z0-9]{5,19}$/
				if(!wxId){ //&&!wxreg.test(wxId)
					this.$u.toast('微信号码不能为空')
					return false;
				}
				if(contactNumber&&!this.$u.test.mobile(contactNumber)){
					this.$u.toast('手机号码不正确')
					return false;
				}
				if(!nickname){
					this.$u.toast('名称不能为空')
					return false;
				}
				if(!address){
					this.$u.toast('地址不能为空')
					return false;
				}
				const {firstLogin,type,fromPage} = this.option
				// if(this.option.type="want"&&!this.loginForm.userAddressCount&&this.pageUserType==='store'&&!firstLogin){
				// 	//意向页面的跳转 并且不是入驻页面
				// 	this.$u.toast('收货地址不能为空')
				// 	return false;
				// }
				this.loading = true;
				// if(firstLogin){
				// 	//首次登录首先需要设置身份
				// 	await updateUserType({
				// 		uid:this.loginForm.uid,
				// 		operationStep:1, //设置用户步骤
				// 		userType:this.pageUserType
				// 	})
				// }
				await userEnterInfoSetting({
					...this.form,
					operationStep:2, //完成资料录入
					businessYear:this.form.yearId,
					userType:this.pageUserType
				}).then(res=>{
					this.loading = false;
					this.$u.toast('操作成功')
					this.$store.dispatch('getInfo');
					// 如果不是首次登录
					if(!firstLogin){
						//判断进来的页面 是否是体验商城  如果是  入驻完成直接跳往认证页面
						if(fromPage && fromPage=='experienceStore'){
							setTimeout(()=>{
									this.$u.route({
									type:"redirectTo",
									url:"/pagesA/authPage/authPage",
										params: {
											label: '店家认证',
											authType: 3
										}
								})
							},1000)
						}else{
							setTimeout(()=>{
								this.$u.route({
									type:'back'
								})
							},1000)
						}
					}else{
						//首次登录注册 判断是门店则引导去完善偏好
						if(this.pageUserType==='store'){
							uni.showModal({
								content:'是否现在去设置门店偏好？',
								confirmText:"是",
								cancelText:'否',
								title:'温馨提示',
								success:({confirm})=>{
									if(confirm){
										this.$u.route({
											type:"reLaunch",
											url:'/pagesB/like/like',
											params:{
												isLogin:1 //是否为登录到偏好设置界面
											}
										})
									}else{
										this.$u.route({
											type:"reLaunch",
											url:'/pages/index/index'
										})
									}
								}
							})
						}else{
							this.$u.route({
								type:"reLaunch",
								url:'/pages/index/index'
							})
						}
					}
				}).catch(()=>{
					this.loading = false;
				})
			},
			goAddress(){
				this.$u.route({
					url:'/pagesB/address/address'
				})
			},
			//获取经营年数id
			getYearIds(item){
				this.$set(this.form,'yearId',item)
			},
			//选择酒店地址
			chooseAddress(){
				uni.chooseLocation({
				type: 'gcj02',
					success: res => {
						this.$set(this.form,'hotelAddress',res.address)
					},
					fail: res =>  {
						if(res.errMsg==='chooseLocation:fail auth deny') this.$u.toast('您已拒绝授权，请打开设置允许')
					}
				})
			},
			//上传的图片
			getImgStr(str){
				this.form.picture = str;
			},
			//监听上传图片
			getUpload(flag){
				this.uploadFlag = flag;
			}
		}
	}
</script>

<style lang="scss" scoped>

.save-btn{
	padding: 100rpx 0;
}
.form-label{
	width: 210rpx;
	color: white;
	font-size: 32rpx;
}
.hotel-address{
	padding: 0px 10px;
	border:1px solid rgb(66, 63, 79);
	background-color: #161324;
	text-align: left;
	overflow: hidden;
	white-space: nowrap;  
	height: 70rpx;
	line-height: 70rpx;
	box-sizing: border-box;
	color: white;
	border-radius: 8rpx;
}
.btn-style{
	width: 260rpx;
	line-height: 70rpx;
	text-align: center;
	margin: 0 auto;
	height: 70rpx;
	border-radius: 50rpx;
	background-color: #765CFE;
	color: white;
}
.history-list{
	margin-top: 48rpx;
		.history-item{
			position: relative;
			&::after{
				content: "";
				position: absolute;
				width: 1rpx;
				height: 26rpx;
				top: 50%;
				margin-top: -13rpx;
				background-color: #B0B2D8;
				right: 21%;
			}
		}
	}
</style>
